﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using Font = System.Drawing.Font;
using Image = System.Drawing.Image;
using Rectangle = System.Drawing.Rectangle;

namespace ECC_EXT_ImageUtility.ImageLib
{
    public class ImageTools
    {

        public void CombinePDFImage(string pdfSrc,Image img,string outputPath)
        {
            //var outputStream =
            //    File.Create(pdfSrc);

            //Document document = new Document();
            //PdfWriter.GetInstance(document, outputStream);
            //document.Open();

            //var table1 = new PdfPTable(1);
   
            //var inputImage = iTextSharp.text.Image.GetInstance(img,ImageFormat.Tiff);

            //inputImage.SetAbsolutePosition(0, 0);
            //inputImage.Alignment = iTextSharp.text.Image.UNDERLYING;

            //document.Add(table1);
            //document.Add(inputImage);

            //document.Close();

            Document document = new Document();
            PdfWriter writer = PdfWriter.GetInstance(
            document, File.Create(outputPath));
            document.Open();

            PdfPTable table = new PdfPTable(1);
            PdfReader reader = new PdfReader(pdfSrc);
            int n = reader.NumberOfPages;
            PdfImportedPage page;

            page = writer.GetImportedPage(reader, 1);
            var vIamge = iTextSharp.text.Image.GetInstance(page);
            var mImage = iTextSharp.text.Image.GetInstance(img,ImageFormat.Tiff);

            mImage.SetAbsolutePosition(-12, -10);
            vIamge.SetAbsolutePosition(0, 0);
            mImage.ScaleToFit(vIamge.Width + 80, vIamge.Height + 15);

            document.Add(mImage);
            document.Add(vIamge);

            document.Close();

        }

        public Image CombineImage(Image srcImage,
                                  Image waterMarkImage,
                                  int posX,int posY)
        {
            using (var graphicHandle = this.CreateGrapgic(srcImage))
            {
                this.GraphicsDraw(graphicHandle, waterMarkImage,
                                  new Rectangle(posX, posY, waterMarkImage.Width, waterMarkImage.Height));
            }
            
            return srcImage;
        }


        public Image CombineImage(string baseSrc,
                                 string savePath,
                                 string waterMarkSrc,
                                 int posX,int posY)
        {
            var srcImage = Image.FromFile(baseSrc);
            var waterMarkImage = Image.FromFile(waterMarkSrc);
            var graphicHandle = this.CreateGrapgic(srcImage);
            graphicHandle = this.GraphicsDraw(graphicHandle, waterMarkImage,
                                              new Rectangle(posX, posY, waterMarkImage.Width, waterMarkImage.Height));
            srcImage.Save(savePath);
            graphicHandle.Dispose();
            srcImage.Dispose();
            waterMarkImage.Dispose();

            return srcImage;
        }

        private bool FitSize(Graphics graphicHandle,Size size,ref string waterMarkText,ref Font font,float minFontSize)
        {

            var tmpWaterMarkText = waterMarkText;
            SizeF vSize;
            while(font.Size > minFontSize)
            {
                vSize =
                    graphicHandle.MeasureString(waterMarkText, font);

                if (vSize.Height >= size.Height)
                {
                    font =
                        new Font(font.FontFamily, font.Size - 1);
                    waterMarkText = tmpWaterMarkText;
                    continue;
                }

                if (vSize.Width >= size.Width)
                {
                    var tmpStrs =
                        waterMarkText.Split(new char[]{'\n'});


                    for(int i = 0; i < tmpStrs.Length; i++)
                    {
                        vSize =
                            graphicHandle.MeasureString(tmpStrs[i], font);
                        if (vSize.Width >= size.Width)
                        {
                            var rt = size.Width / vSize.Width;
                            int idx = (int)Math.Floor(rt*tmpStrs[i].Length);
                            tmpStrs[i] = 
                                tmpStrs[i].Insert(idx, "\n");
                        }

                    }
                    waterMarkText = "";
                    foreach (var tmpStr in tmpStrs)
                    {
                        waterMarkText = waterMarkText+tmpStr+"\n";
                    }
                    //原始换行不动，超出的行再换行

                    continue;
                }

                return true;

            }
            return false;
        }


        private void ShrinkFont()
        {
        }

        //private IList<>


        public Image CombineText(Image srcImage, string waterMarkText, int posX, int posY,Size recSize,float minFontSize)
        {
            
            using (var graphicHandle = this.CreateGrapgic(srcImage))
            {

                Rectangle rec = new Rectangle();

                var vFont = new Font("ＭＳ ゴシック", 50);

                this.FitSize(graphicHandle, recSize, ref waterMarkText, ref vFont, minFontSize);


                var vSize =
                    graphicHandle.MeasureString(waterMarkText, vFont);


                var waterMarkImage =
                    this.CreateTextWaterMark(waterMarkText, vSize, vFont);


                this.GraphicsDraw(graphicHandle, waterMarkImage,
                                  new Rectangle(posX, posY, waterMarkImage.Width, waterMarkImage.Height));

                waterMarkImage.Dispose();    
            }
            return srcImage;
            
        }

        public void CombineText(string baseSrc,
                                string savePath,
                                string waterMarkText,
                                int posX, int posY)
        {
            var srcImage = Image.FromFile(baseSrc);
            //var waterMarkImage = Image.FromFile(waterMarkSrc);
            var graphicHandle = this.CreateGrapgic(srcImage);

            var vFont = new Font("ＭＳ ゴシック", 50);
            var vSize =
                graphicHandle.MeasureString(waterMarkText,vFont);


            var waterMarkImage = 
                this.CreateTextWaterMark(waterMarkText, vSize, vFont);


            graphicHandle = this.GraphicsDraw(graphicHandle, waterMarkImage,
                                              new Rectangle(posX, posY, waterMarkImage.Width, waterMarkImage.Height));

            srcImage.Save(savePath);
            graphicHandle.Dispose();
            srcImage.Dispose();
            waterMarkImage.Dispose();
        }

        private Graphics CreateGrapgic(Image baseImage)
        {
            var myGraphics = Graphics.FromImage(baseImage);

            return myGraphics;
        }

        private Graphics GraphicsDraw(Graphics bGraphics, Image wImage, Rectangle rectangle)
        {
            bGraphics.DrawImage(wImage,
                                rectangle);
            bGraphics.Save();
            return bGraphics;
        }

        private Image CreateTextWaterMark(String watermarkText,SizeF size,Font font)
        {


            var baseImage = new Bitmap((int)size.Width+5, (int)size.Height+5);

            var gHandle = Graphics.FromImage(baseImage);

            //.FillRectangle(Brushes.White, 0, 0, floatBmp.Width, floatBmp.Height);

            gHandle.DrawString(watermarkText, font,Brushes.Black,5,5);
            gHandle.Save();

            //baseImage.MakeTransparent(Color.White);

            return baseImage;
        }
    }
}
